package com.leetcode.string;

import java.util.Deque;
import java.util.LinkedList;

/**
 * date:
 * description:
 */
public class RemoveDigits {
    public static String removeKdigits(String num, int k) {
        Deque<Character> queue = new LinkedList<>();
        int remove = k;
        for(int i = 0; i < num.length(); i++){
            char ch = num.charAt(i);
            while(!queue.isEmpty() && remove > 0 && queue.peekLast() > ch){
                queue.pollLast();
                remove--;
            }
            queue.offerLast(ch);
        }
        for(int i = 0; i < remove; i++){
            queue.pollLast();
        }
        StringBuilder sb = new StringBuilder();
        boolean zeroFlag = true;
        while(!queue.isEmpty()){
            char ch = queue.pollFirst();
            if(zeroFlag && ch == '0'){
                continue;
            }
            zeroFlag = false;
            sb.append(ch);
        }
        if(sb.length() == 0){
            return "0";
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String num = "10";
        String res = removeKdigits(num, 2);
        System.out.println(res);
    }
}
